
***************************************************
*Figure A.2
***************************************************
***************************************************
*Figure A.2, Age in quarters
***************************************************

use "$workdata/baseline", clear /* see $data_do/baseline.do */

capture drop age_q
g age_q=floor( (age-floor(age))*4)*(1/4) + floor(age)

g b_month=month(foed_dag)
g b_year=year(foed_dag)

collapse wage sa (sum) e, by(pnr age_q ) 

capture drop age_month_q_y
g age_month_q_y=floor(age_q)
tostring age_month_q_y, replace
capture drop age_month_q_m
g age_month_q_m=(round((age_q-floor(age_q))*12)+1)/100
tostring age_month_q_m, replace
capture drop age_month_q
egen age_month_q=concat(age_month_q_y age_month_q_m)
destring age_month_q, replace
local resp="age_month_q>=25 & age_month_q<=25.05"
g resp=`resp'
g d=age_q>=25
g age_q0=age_q-25
g age_q0_2= age_q0^2
g age_q0_3= age_q0^3
g dXage_q0=d*age_q0
g dXage_q0_2=d*age_q0_2
g dXage_q0_3=d*age_q0_3

*********************************************************************


eststo clear
local model=0
foreach y of varlist  sa {

preserve
collapse `y' if age_q>=21 & age_q<29, by(pnr age_q)
sum `y'
local obs=(r(N))
di `obs'
restore

forvalues s=2/2  {
preserve
keep if age_q>=21 & age_q<29

if `y'==sa   local initial=0.13
di `initial'

local spec_num=`s'
if `y'==sa   local title="Social Assistance"
local min_wage=-0.05
local max_wage=0.05
local min_sa  =0.09
local max_sa  =0.19
if `spec_num'==1 local spec="d age_q0                   dXage_q0"
if `spec_num'==2 local spec="d age_q0 age_q0_2          dXage_q0 dXage_q0_2"
if `spec_num'==3 local spec="d age_q0 age_q0_2 age_q0_3 dXage_q0 dXage_q0_2 dXage_q0_3"
if `spec_num'==4 local spec="d age_q0"
if `spec_num'==5 local spec="d age_q0 age_q0_2"
if `spec_num'==6 local spec="d age_q0 age_q0_2 age_q0_3"
if `spec_num'==1 local spec_name="1st order polynomial spline"
if `spec_num'==2 local spec_name="2nd order polynomial spline"
if `spec_num'==3 local spec_name="3rd order polynomial spline"
if `spec_num'==4 local spec_name="1st order polynomial"
if `spec_num'==5 local spec_name="2nd order polynomial"
if `spec_num'==6 local spec_name="3rd order polynomial"
local model =`model'+1
local repl_app="append"
if `model'==1 local repl_app="replace"
eststo model`model': reg `y' `spec' [aw=e] /*if resp==0*/ , vce(cluster pnr)

predict yhat
local beta_d=round(_b[d], .001)*10000
local beta_d=floor(`beta_d')/100 
collapse `y' yhat (sem) se=`y'  [aw=e], by(age_q)

g `y'_min=`y'-se*1.96
g `y'_max=`y'+se*1.96
capture drop age_month_q_y
g age_month_q_y=floor(age_q)
tostring age_month_q_y, replace
capture drop age_month_q_m
g age_month_q_m=(round((age_q-floor(age_q))*12)+1)/100
tostring age_month_q_m, replace
capture drop age_month_q
egen age_month_q=concat(age_month_q_y age_month_q_m)
destring age_month_q, replace

twoway (rarea `y'_min `y'_max age_q if age_month_q<25  /*24.12*/, color(gs13)) ///
	   (rarea `y'_min `y'_max age_q if age_month_q>=25 /*25.04*/, color(gs13)) ///
	   (line yhat age_q if age_q< 25 & `y'>`min_`y'' & `y'<`max_`y'', lcolor(gs3)) ///
   	   (line yhat age_q if age_q>= 25 & `y'>`min_`y'' & `y'<`max_`y'', lcolor(gs3))  ///
	   (scatter `y' age_q if age_q< 25 & `y'>`min_`y'' & `y'<`max_`y'', msymbol(o) mlcolor(gs3) mfcolor(gs14))  ///
	   (scatter `y' age_q if age_q>=25 & `y'>`min_`y'' & `y'<`max_`y'', msymbol(o) mlcolor(gs3) mfcolor(gs14))  ///
	    , ylabel(`min_`y''(0.02)`max_`y'') ytitle(`title', height(5))  ///
	     xlabel(21(1)29)        xtitle("Age in quarters", height(5))      ///
	     xline(24.875 ) scheme(s1mono) legen(off) name(figA2_1, replace) note("Effect size: `beta_d' pp (Specification: `spec_name')")


graph save figA2_1  $figures/figA2_1, replace
graph export $figures/figA2_1.pdf, name(figA2_1) replace
graph export $figures/figA2_1.png, name(figA2_1) replace
 
restore
}
}

***************************************************
*Figure A.2, Age in semesters
***************************************************

use "$workdata/baseline", clear /* see $data_do/baseline.do */

capture drop age_q
g age_q=floor( (age-floor(age))*2)*(1/2) + floor(age)

g b_month=month(foed_dag)
g b_year=year(foed_dag)

collapse wage sa (sum) e, by(pnr age_q ) 

capture drop age_month_q_y
g age_month_q_y=floor(age_q)
tostring age_month_q_y, replace
capture drop age_month_q_m
g age_month_q_m=(round((age_q-floor(age_q))*12)+1)/100
tostring age_month_q_m, replace
capture drop age_month_q
egen age_month_q=concat(age_month_q_y age_month_q_m)
destring age_month_q, replace
local resp="age_month_q>=25 & age_month_q<=25.05"
g resp=`resp'
g d=age_q>=25
g age_q0=age_q-25
g age_q0_2= age_q0^2
g age_q0_3= age_q0^3
g dXage_q0=d*age_q0
g dXage_q0_2=d*age_q0_2
g dXage_q0_3=d*age_q0_3

*********************************************************************


eststo clear
local model=0
foreach y of varlist  sa {

preserve
collapse `y' if age_q>=21 & age_q<29, by(pnr age_q)
sum `y'
local obs=(r(N))
di `obs'
restore

forvalues s=2/2  {
preserve
keep if age_q>=21 & age_q<29

if `y'==sa   local initial=0.13
di `initial'

local spec_num=`s'
if `y'==sa   local title="Social Assistance"
local min_wage=-0.05
local max_wage=0.05
local min_sa  =0.09
local max_sa  =0.19
if `spec_num'==1 local spec="d age_q0                   dXage_q0"
if `spec_num'==2 local spec="d age_q0 age_q0_2          dXage_q0 dXage_q0_2"
if `spec_num'==3 local spec="d age_q0 age_q0_2 age_q0_3 dXage_q0 dXage_q0_2 dXage_q0_3"
if `spec_num'==4 local spec="d age_q0"
if `spec_num'==5 local spec="d age_q0 age_q0_2"
if `spec_num'==6 local spec="d age_q0 age_q0_2 age_q0_3"
if `spec_num'==1 local spec_name="1st order polynomial spline"
if `spec_num'==2 local spec_name="2nd order polynomial spline"
if `spec_num'==3 local spec_name="3rd order polynomial spline"
if `spec_num'==4 local spec_name="1st order polynomial"
if `spec_num'==5 local spec_name="2nd order polynomial"
if `spec_num'==6 local spec_name="3rd order polynomial"
local model =`model'+1
local repl_app="append"
if `model'==1 local repl_app="replace"
eststo model`model': reg `y' `spec' [aw=e]  , vce(cluster pnr)

predict yhat
local beta_d=round(_b[d], .001)*10000
local beta_d=floor(`beta_d')/100 
collapse `y' yhat (sem) se=`y'  [aw=e], by(age_q)

g `y'_min=`y'-se*1.96
g `y'_max=`y'+se*1.96
capture drop age_month_q_y
g age_month_q_y=floor(age_q)
tostring age_month_q_y, replace
capture drop age_month_q_m
g age_month_q_m=(round((age_q-floor(age_q))*12)+1)/100
tostring age_month_q_m, replace
capture drop age_month_q
egen age_month_q=concat(age_month_q_y age_month_q_m)
destring age_month_q, replace

twoway (rarea `y'_min `y'_max age_q if age_month_q<25  /*24.12*/, color(gs13)) ///
	   (rarea `y'_min `y'_max age_q if age_month_q>=25 /*25.04*/, color(gs13)) ///
	   (line yhat age_q if age_q< 25 & `y'>`min_`y'' & `y'<`max_`y'', lcolor(gs3)) ///
   	   (line yhat age_q if age_q>= 25 & `y'>`min_`y'' & `y'<`max_`y'', lcolor(gs3))  ///
	   (scatter `y' age_q if age_q< 25 & `y'>`min_`y'' & `y'<`max_`y'', msymbol(o) mlcolor(gs3) mfcolor(gs14))  ///
	   (scatter `y' age_q if age_q>=25 & `y'>`min_`y'' & `y'<`max_`y'', msymbol(o) mlcolor(gs3) mfcolor(gs14))  ///
	    , ylabel(`min_`y''(0.02)`max_`y'') ytitle(`title', height(5))  ///
	     xlabel(21(1)29)        xtitle("Age in semesters", height(5))      ///
	     xline(24.75) scheme(s1mono) legen(off) name(figA2_2, replace) note("Effect size: `beta_d' pp (Specification: `spec_name')")


graph save figA2_2  $figures/figA2_2, replace
graph export $figures/figA2_2.pdf, name(figA2_2) replace
graph export $figures/figA2_2.png, name(figA2_2) replace
 
restore
}
}

graph use $figures/figA2_1
graph use $figures/figA2_2
graph combine ///
      figA2_1 figA2_2 ///
      , cols(2) rows(1) ysize(4) xsize(11) altshrink  scheme(s1mono) ///
	  name(figA2, replace)
graph save figA2  $figures/figA2, replace
graph export $figures/figA2.pdf, name(figA2) replace
graph export $figures/figA2.png, name(figA2) replace


***************************************************
* end: Figure A.2
***************************************************
